package com.example.administrator.officeplatform.kj;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.administrator.officeplatform.MyApplication;
import com.example.administrator.officeplatform.R;
import com.example.administrator.officeplatform.adapter.AuditorAdapter;
import com.example.administrator.officeplatform.adapter.TimeSearchAdapter;
import com.example.administrator.officeplatform.base.BaseActivity;
import com.example.administrator.officeplatform.bean.Auditorinfo;
import com.example.administrator.officeplatform.bean.TimeInfo;
import com.example.administrator.officeplatform.utils.JsonUtil;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/12/25.
 */

public class AuditorActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rv)
    RecyclerView rv;
    private Context mContext;
    private List<RelativeLayout> ls_rl;
    private List<View> ls_v;
    private List<Auditorinfo> ls_audit = new ArrayList<>();
    private AuditorAdapter personAdapter;
    private OkHttpClient mOkHttpClient;
    private String index;
    private ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_time);

    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mContext=this;
        //设置布局管理器
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setHasFixedSize(true);
        rv.setItemAnimator(new DefaultItemAnimator());

    }

    @Override
    protected void initData() {
        toolbar.setTitle("审核人");
        index=(String)getIntent().getExtras().get("index");
        ls_audit= new ArrayList<>();
        search();
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void search(){
        mProgressDialog = ProgressDialog.show(mContext, null, "正在查询中...");
        mProgressDialog.setCancelable(true);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mOkHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(IpUtil.getIP()+"/reimbursement/workflowConfig/selectAuditors/"+index)
                .header("cookie", MyApplication.getMsession())
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mProgressDialog.dismiss();

                        Toast.makeText(mContext, "连接异常", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                mProgressDialog.dismiss();
                String str = response.body().string();
                if (str.indexOf("contentList") >= 0) {
                    String contentList = JsonUtil.getMsg(str, "contentList");
                    final List<Auditorinfo> lsMap = JsonUtil.getPersons(contentList, new TypeToken<List<Auditorinfo>>() {
                    }.getType());
                    ls_audit.addAll(lsMap);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(ls_audit.size()==0){
                                Toast.makeText(mContext, "未查询到审核人信息", Toast.LENGTH_SHORT).show();
                            }

                            if (personAdapter == null) {
                                personAdapter = new AuditorAdapter(ls_audit, mContext);
                                rv.setAdapter(personAdapter);
                            } else {
                                personAdapter.notifyDataSetChanged();
                            }
                            personAdapter.setOnRecyclerViewListener(new AuditorAdapter.OnRecyclerViewListener() {
                                @Override
                                public void onItemClick(int position) {
                                    Intent mIntent = new Intent();
                                    Auditorinfo bmInfo = ls_audit.get(position);
                                    //把返回数据存入Intent
                                    mIntent.putExtra("name", bmInfo.getName());
                                    mIntent.putExtra("id", bmInfo.getId());
                                    //设置返回数据
                                    setResult(8, mIntent);
                                    finish();
                                }

                            });

                        }
                    });
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(mContext, "获取审核人失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });


    }
}
